package com.akkainaction.helloactor.message.actor.ask;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.dispatch.OnFailure;
import akka.dispatch.OnSuccess;
import akka.pattern.Patterns;
import akka.util.Timeout;
import scala.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class AskMain {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        ActorRef askDemoActor = system.actorOf(Props.create(AskDemoActor.class), "askDemoActor");
        Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
        //异步处理
        Future<Object> f = Patterns.ask(askDemoActor, "Akka Ask", timeout);
        //......
        //这里可以写一些不依赖future的业务逻辑
        //......

        //成功
        f.onSuccess(new OnSuccess<Object>() {
            @Override
            public void onSuccess(Object result) {
                System.out.println("处理成功,收到消息:" + result);
            }
        }, system.dispatcher());

        //失败
        f.onFailure(new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                System.out.println("处理失败,异常信息:" + throwable);
            }
        }, system.dispatcher());
    }
}
